﻿using System.ComponentModel.Composition;
using System.IO;
using DeepZoomPivotConstructor.VisualLib;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using RenderEngine;
using RenderEngineInterfaces;
using SimpleFileRendering;

namespace t_SimpleFileRendering
{
    /// <summary>
    /// Tests out the jpeg render guy - which just does a quick translation here.
    /// </summary>
    [TestClass]
    public class t_JPEGFileRender
    {
        public t_JPEGFileRender()
        {
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        [TestInitialize]
        public void InitForTest()
        {
            t_TestingLibrary.Utilities.MyClassInit();
            t_TestingLibrary.Utilities.AddPart(new RenderControl());
            t_TestingLibrary.Utilities.AddPart(new LayoutHelper());
        }

        [TestCleanup]
        public void ShutDownTest()
        {
            t_TestingLibrary.Utilities.MyClassDone();
        }

        [TestMethod]
        public void TestNonExistantFile()
        {
            JPEGFileRender r = new JPEGFileRender();
            Assert.AreEqual(RenderPluginPriority.RPPCantDoIt, r.RenderPriority(new FileInfo("stupiddude.ps")));
            Assert.AreEqual(RenderPluginPriority.RPPCantDoIt, r.RenderPriority(new FileInfo("stupiddude.jpeg")));
        }

        [TestMethod]
        public void TestExistingFileWrongType()
        {
            JPEGFileRender r = new JPEGFileRender();
            FileInfo f = new FileInfo(@"t_JPEGFileRender.cs");
            using (var output = f.CreateText())
            {
                output.WriteLine("output");
                output.Close();
            }

            Assert.IsTrue(f.Exists, "File to test on does not exist");
            Assert.AreEqual(RenderPluginPriority.RPPCantDoIt, r.RenderPriority(f));
        }

        [TestMethod]
        [DeploymentItem("wallpaper.jpg")]
        public void TestExistingFile()
        {
            JPEGFileRender r = new JPEGFileRender();
            FileInfo f = new FileInfo(@"wallpaper.jpg");
            Assert.IsTrue(f.Exists, "File to test on does not exist");
            Assert.AreEqual(RenderPluginPriority.RPPGeneric, r.RenderPriority(f));
        }

        [Import]
        private IRenderEngine MyRender { get; set; }

        [TestMethod]
        [DeploymentItem("wallpaper.jpg")]
        public void TestRendering()
        {
            JPEGFileRender r = new JPEGFileRender();
            FileInfo f = new FileInfo(@"wallpaper.jpg");
            var vNext = r.Render(f);
            Assert.IsNotNull(vNext, "Future should not have come back null");
            Visual v = vNext.Result;
            Assert.IsNotNull(v, "Visual shouldn't have been null");
            Assert.AreEqual(1, v.SGNodeList.Count, "Unexpected number of actual images");
            Assert.IsNotNull(v.Info, "Was not expecting the visual info to be null");
            Assert.IsNotNull(v.Info.Title, "Was not expecting the title to be null");
            Assert.AreEqual("wallpaper", v.Info.Title, "Title is not correct");
            Assert.IsNull(v.Info.Info, "There shouldn't be any info messages for this file!");
        }

        [TestMethod]
        [DeploymentItem("wallpaper.jpg")]
        public void TestMetaData()
        {
            t_TestingLibrary.Utilities.AddPart(new JPEGFileRender());
            t_TestingLibrary.Utilities.Compose(this);
            FileInfo f = new FileInfo(@"wallpaper.jpg");
            Visual v = MyRender.Render(f);
            Assert.IsNotNull(v, "Visual should have been rendered!");
            Assert.AreEqual(1, v.SGNodeList.Count, "Expected a single image to come back!");
        }

        [TestMethod]
        [DeploymentItem("funydpi.png")]
        public void TestPNGWithBadDPI()
        {
            JPEGFileRender r = new JPEGFileRender();
            FileInfo f = new FileInfo(@"funydpi.png");
            var vNext = r.Render(f);
            Assert.IsNotNull(vNext, "Visual should have been rendered!");
            var output = vNext.Result;
            Assert.IsNull(output, "Expected the multi-DPI image to come back with no rendering!");
        }
    }
}
